package top.lishuoboy.cloudstreamprovider8801.provider.impl;

import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import top.lishuoboy.cloudstreamprovider8801.provider.IMessageProvider;
import top.lishuoboy.dependency.base.json.FastjsonUtil;
import top.lishuoboy.dependency.base.json.HuJsonUtil;
import top.lishuoboy.dependency.base.str.MyStrPool;


@Slf4j
@EnableBinding(Source.class) //定义消息的推送管道
public class MessageProviderImpl implements IMessageProvider {
    @Autowired
    @Qualifier("output")
    private MessageChannel outMChannel; // 消息发送管道

    @Override
    public String send() {
        /**
         * outMChannel=={
         * 	"applicationContextId":"application",
         * 	"beanDescription":"bean 'output'",
         * 	"beanName":"output",
         * 	"componentName":"output",
         * 	"componentType":"channel",
         * 	"conversionService":{},
         * 	"countsEnabled":true,
         * 	"errorRate":{
         * 		"count":0,
         * 		"countLong":0,
         * 		"max":0.0,
         * 		"mean":0.0,
         * 		"min":0.0,
         * 		"standardDeviation":0.0
         *        },
         * 	"expression":null,
         * 	"fullChannelName":"application.output",
         * 	"integrationPatternType":"message_channel",
         * 	"interceptors":[
         *        {}
         * 	],
         * 	"loggingEnabled":true,
         * 	"maxSendDuration":0.0,
         * 	"meanErrorRate":0.0,
         * 	"meanErrorRatio":0.0,
         * 	"meanSendDuration":0.0,
         * 	"meanSendRate":0.0,
         * 	"minSendDuration":0.0,
         * 	"overrides":{
         * 		"countsConfigured":true,
         * 		"loggingConfigured":true,
         * 		"metricsConfigured":true,
         * 		"statsConfigured":true
         *    },
         * 	"sendCount":0,
         * 	"sendCountLong":0,
         * 	"sendDuration":{
         * 		"count":0,
         * 		"countLong":0,
         * 		"max":0.0,
         * 		"mean":0.0,
         * 		"min":0.0,
         * 		"standardDeviation":0.0
         *    },
         * 	"sendErrorCount":0,
         * 	"sendErrorCountLong":0,
         * 	"sendRate":{
         * 		"count":0,
         * 		"countLong":0,
         * 		"max":0.0,
         * 		"mean":0.0,
         * 		"min":0.0,
         * 		"standardDeviation":0.0
         *    },
         * 	"standardDeviationSendDuration":0.0,
         * 	"statsEnabled":true,
         * 	"subscriberCount":1,
         * 	"timeSinceLastSend":0.0
         * }
         */
        log.trace("outMChannel=={}", FastjsonUtil.toJsonPrettyStr(outMChannel));
        String uuid = IdUtil.randomUUID();
        /**
         * message=={
         * 	"headers":{
         * 		"id":"466d9cd0-f099-94d7-8d29-8f84e213dfcb",
         * 		"timestamp":1651243794655
         *        },
         * 	"payload":"ebdfd59a-df95-4f44-af63-2ded520ae25e"
         * }
         */
        Message<String> message = MessageBuilder.withPayload(uuid).build();
        log.trace("message=={}", HuJsonUtil.toJsonPrettyStr(message));
        boolean sendBool = outMChannel.send(MessageBuilder.withPayload(uuid).build());
        log.warn(MyStrPool.LOG_PRE + "sendBool=={}, uuid=={}", sendBool, uuid);
        return uuid;
    }
}
